package com.huawei.file;

import java.io.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

public class UserFileimpl implements UserFile{
    public static final String SEPARATE_FIELD = "\n";// 换行
    public static final String SEPARATE_LINE = "\t"; // 分隔
    @Override
    public void resultIn(Result result) {
        String name = "对局记录.txt";
        InputStream in = null;
        try {
            in = new FileInputStream(name);// 判断本地是否存在此文件
            if (in != null) {
                in.close();// 关闭输入流
                // 可获取输入流，则存在文件，采取修改文件方式
                createFile(name, true, result);
            }
        } catch (FileNotFoundException e) {
            // 输入流获取失败，则不存在文件，采取新建新文件方式
            createFile(name, false, result);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public Result addResult(String winnerName,int winLose,int playround, int gameround) {
        char wl=' ';
        switch (winLose){
            case 1:wl='胜';break;
            case 2:wl='平';break;
            case 3:wl='负';break;
        }
        Date date =new Date();
        DateFormat format =new SimpleDateFormat("yyyyMMdd");
        String time =format.format(date).toString();
        Result result=new Result(time,winnerName,wl,playround,gameround);
        return result;
    }

    @Override
    public void createFile(String name, boolean label, Result result) {
        BufferedOutputStream out = null;
        StringBuffer sbf = new StringBuffer();// 拼接内容
        try {
            if (label) {// 当已存在当天的文件，则在文件内容后追加
                // 创建输出流，用于追加文件
                out = new BufferedOutputStream(new FileOutputStream(name, true));
            } else {// 不存在当天文件，则新建文件
                // 创建输出流，用于保存文件
                out = new BufferedOutputStream(new FileOutputStream(name));
            }
            sbf.append("时间：").append(result.time);
            sbf.append("昵称：").append(result.playerName).append(SEPARATE_LINE);
            sbf.append("结果：").append(result.winLose).append(SEPARATE_LINE);
            sbf.append("胜场：").append(result.playround).append(SEPARATE_LINE);
            sbf.append("总对局数：").append(result.gameround).append(SEPARATE_FIELD);
            String str = sbf.toString();
            byte[] b = str.getBytes();
            for (int i = 0; i < b.length; i++) {
                out.write(b[i]);// 将内容写入本地文件
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (out != null)
                    out.close();// 关闭输出流
            } catch (Exception e2) {
                e2.printStackTrace();
            }
        }
    }
    @Override
    public String resultOut() throws Exception{
        FileInputStream in = new FileInputStream("对局记录.txt");
        byte[] b = new byte[in.available()];
        in.read(b);
        String str=new String(b);
        in.close();
        return str;
    }
    public  void clearInfoForFile() {
        String fileName="对局记录.txt";
        File file =new File(fileName);
        try {
            if(!file.exists()) {
                file.createNewFile();
            }
            FileWriter fileWriter =new FileWriter(file);
            fileWriter.write("");
            fileWriter.flush();
            fileWriter.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
